package com.wu.clone;

import java.util.HashMap;
import java.util.Map;

/**
 * 复杂链表的复制
 */
class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}

public class Solution {
    public RandomListNode Clone(RandomListNode pHead) {
        Map<RandomListNode, RandomListNode> map = new HashMap<>();
        RandomListNode tem = pHead;
        while (tem != null) {
            RandomListNode node = new RandomListNode(tem.label);
            map.put(tem, node);
            tem = tem.next;
        }
        tem = pHead;
        while (tem != null) {
            RandomListNode node = map.get(tem);
            node.next = map.get(tem.next);
            node.random = map.get(tem.random);
            tem = tem.next;
        }
        return map.getOrDefault(pHead, null);
    }
}